"""
使用unittest框架，结合requests库，在框架中完成tpshop前台用户注册，添加 “注册成功” 断言。
    1.请求方法: POST
    2.请求头:Content-Type: application/x-www-form-urlencoded
    3.请求体参数(手机注册)
    username:
    verify_code:
    password:
    password2:
    invite: 可选参数
注册成功: {'status': 1, 'msg': '注册成功', 'result':用户信息}
注册手机号已存在 : {'status': -1, 'msg': '该手机号已注册', 'result': ''}
两次输入密码不一致:{'status': -1, 'msg': '两次密码不一致', 'result': ''}
发送验证码的URL:
URL: http://hmshop-test.itheima.net/index.php?m=Home&c=User&a=verify&type=user_reg&r=0.19333579479986707

注册页面的  URL: http://hmshop-test.itheima.net/Home/user/reg.html
已注册手机号的UPL: http://hmshop-test.itheima.net/Home/User/reg.html
两次密码输入不一致的 URL: http://hmshop-test.itheima.net/Home/User/reg.html

"""
import requests
import unittest


# 定义测试类
class TestRegister(unittest.TestCase):
    # 注册成功
    def test01_register(self):
        # 1.创建Session 实例
        session = requests.Session()
        # 2.使用Session 实例,调用get方法,发送获取验证码请求, 得到响应结果
        # 浏览器开发者工具,获取验证码链接
        url_verify = "http://hmshop-test.itheima.net/index.php?m=Home&c=User&a=verify&type=user_reg&r=0.19333579479986707"
        session.get(url=url_verify)
        # 3.使用Session 实例调用post方法,发送注册请求
        # 浏览器开发者工具,获取注册链接
        url_regiter = "http://hmshop-test.itheima.net/Home/user/reg.html"
        header = {"Content-Type": "application/x-www-form-urlencoded"}
        data = {"username": "19338158871", "verify_code": "8888", "password": "123456", "password2": "123456"}
        resp = session.post(url=url_regiter, headers=header, data=data)
        print(resp.json())
        self.assertEqual(200, resp.status_code)
        self.assertEqual(1, resp.json().get("status"))
        self.assertEqual("注册成功", resp.json().get("msg"))

    # 该手机号已注册
    def test02_register(self):
        session = requests.Session()
        url_verify = "http://hmshop-test.itheima.net/index.php?m=Home&c=User&a=verify&type=user_reg&r=0.19333579479986707"
        session.get(url=url_verify)

        url_regiter = "http://hmshop-test.itheima.net/Home/user/reg.html"
        header = {"Content-Type": "application/x-www-form-urlencoded"}
        data = {"scene": "1", "username": "13012345678", "verify_code": "8888", "password": "123456",
                "password2": "123456"}
        resp = session.post(url=url_regiter, headers=header, data=data)
        self.assertEqual(200, resp.status_code)
        self.assertEqual(-1, resp.json().get("status"))
        self.assertEqual("该手机号已注册", resp.json().get("msg"))

    # 两次密码不一致
    def test03_register(self):
        session = requests.Session()
        url_verify = "http://hmshop-test.itheima.net/index.php?m=Home&c=User&a=verify&type=user_reg&r=0.19333579479986707"
        session.get(url=url_verify)

        url_regiter = "http://hmshop-test.itheima.net/Home/user/reg.html"
        header = {"Content-Type": "application/x-www-form-urlencoded"}
        data = {"username": "17711111119", "verify_code": "8888", "password": "123456", "password2": "12346"}
        resp = session.post(url=url_regiter, headers=header, data=data)
        self.assertEqual(200, resp.status_code)
        self.assertEqual(-1, resp.json().get("status"))
        self.assertEqual("两次密码不一致", resp.json().get("msg"))

